package com.ys.service.finance.impl;

import com.ys.entity.finance.TcwCompanyAccountTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ys.entity.finance.TcwCapitalRegisterClaim;
import com.ys.entity.finance.TcwCapitalRegisterSon;
import com.ys.entity.hr.ThrOperateCompanyBankInfo;
import com.ys.mapper.finance.TcwCapitalRegisterClaimMapper;
import com.ys.service.business.TcwAccountDetailedForCompanyService;
import com.ys.service.finance.TcwCapitalRegisterClaimService;
import com.ys.vo.R;

import java.util.Map;

/**
 * 资金登记认领表
 */
@Service("tcwCapitalRegisterClaimService")
public class TcwCapitalRegisterClaimServiceImpl implements TcwCapitalRegisterClaimService {
	private static final Logger logger = LoggerFactory.getLogger(TcwCapitalRegisterClaimServiceImpl.class);

	@Autowired
	private TcwCapitalRegisterClaimMapper tcwCapitalRegisterClaimMapper;

	@Autowired
	private TcwAccountDetailedForCompanyService tcwAccountDetailedForCompanyService;

	/**
	 * 退回
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R toBack(Long id) throws Exception{
		logger.info("传参:"+id);
		TcwCapitalRegisterClaim entity = new TcwCapitalRegisterClaim().setId(id).queryFirst();
		if(entity!=null && entity.getCapitalRegisterSonId()!=null){
			if(entity.getClaimStatus()!=0){
				return R.error("只有未认领的单据才可退回");
			}
			//新增退回流水
			TcwCapitalRegisterSon sonItem = new TcwCapitalRegisterSon().setId(entity.getCapitalRegisterSonId()).queryFirst();
			ThrOperateCompanyBankInfo thrOperateCompanyBankInfo = new ThrOperateCompanyBankInfo().setId(sonItem.getOperateCompanyBankInfoId()).queryFirst();
			if(sonItem!=null && sonItem.getPbid()!=null && thrOperateCompanyBankInfo!=null && thrOperateCompanyBankInfo.getVid()!=null){
				/**
				 * 新增一条流水
				 * 必填：vgsbm划出公司编码；vgsbmp划入公司编码；vjylx交易类型；fje交易金额；djysj交易时间；dshsj审核时间；vbz备注
				 * 选填：mname;mtype;mnid;mbid;mguid；writeTime记账时间（如果为空，默认审核时间）
				 */
				tcwAccountDetailedForCompanyService.addTcwAccountDetailedForCompanyWithJcNoTrigger(
						"GS0874","挂账账户",thrOperateCompanyBankInfo.getVid(),thrOperateCompanyBankInfo.getVyhzhmc(),
						"运营服务一预付款退回", sonItem.getPaymentAmount().negate(),entity.getCreateTime(), sonItem.getUpdateTime(),"资金登记单："+sonItem.getPbid(),
						null,sonItem.getPaymentTime());
			}else {
				return R.error("未查询到对应的资金登记单信息");
			}

			//修改状态
			entity.setClaimStatus(2);
			tcwCapitalRegisterClaimMapper.update(entity);
		}else {
			return R.error("未查询到需要退回的单据信息");
		}
		return R.ok();
	}

	@Override
	public Map<String,Object> getExtraParam(String bid){
		Map<String,Object> extraParam = new TcwCompanyAccountTransfer()
				.addLeftJoin("YS_ProcInst P ON P.Bid = A.Bid ")
				.addField("FORMAT(P.UpdateTime, 'yyyy-MM-dd HH:mm:ss') AS auditingTime")//审核时间
				.addLeftJoin("TB_CompanyInfo G ON G.vid=A.CompanyVid")
				.addField("G.Vname vName")
				.addLeftJoin("(SELECT ISNULL(SUM(S.PaymentAmount),0) AS paymentAmount,S.Pbid FROM TCW_CapitalRegisterClaimSon S GROUP BY S.Pbid) B ON B.Pbid = A.Bid")
				.addField("B.paymentAmount AS totalAmount")//付款总金额
				.setBid(bid)
				.queryFirstMap();
		return extraParam;
	}


}
